24. Exercise: Private Method

Sphere2

Exercise: Private Method

Abstraction is used to expose only relevant information to the user. By hiding implementation details, we give ourselves flexibility to modify how the program works. In this example, you'll practice abstracting implementation details from the user.

Directions

In this exercise, you'll update the class Sphere so that it becomes possible to change the radius of a sphere after it has been initialized. In order to do this, you'll move the two class invariants into private member functions.

  1. Move the range-check on radius_ into a private member function.
  2. Move the volume_ calculation, which depends on the value of radius_ into the same private member function.
  3. Verify that the class still functions correctly.
  4. Add a mutator method to change the radius of an existing Sphere.
  5. Verify that the mutator method successfully updates both the radius and the volume.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter
  • Opened files (when workspace is loaded): n/a